package com.ilop.sthome.ui.activity.detail;

import android.graphics.drawable.AnimationDrawable;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.view.View;

import com.ilop.sthome.data.greenDao.DefaultBean;
import com.ilop.sthome.ui.adapter.detail.DefaultAdapter;
import com.ilop.sthome.utils.ConfigureUtil;
import com.ilop.sthome.utils.DeviceStatusUtil;
import com.ilop.sthome.utils.greenDao.utils.DefaultDaoUtil;
import com.siterwell.familywellplus.R;
import com.siterwell.familywellplus.databinding.ActivityDetailAlarmBinding;

import java.util.List;

/**
 * @author skygge
 * @date 2020-03-30.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：报警类设备通用（门磁 按键 场景开关 PIR SOS CO SM CAS 水感 热感 复合型 震闪)
 */
public class AlarmDeviceActivity extends BaseDetailActivity<ActivityDetailAlarmBinding>{

    private DefaultAdapter mAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_detail_alarm;
    }

    @Override
    protected void initView() {
        super.initView();
        mDBind.detailIcon.setImageResource(smartDevice.getDrawableResId());
        mDBind.detailTest.setVisibility(View.GONE);
        mAdapter = new DefaultAdapter(mContext);
        mDBind.detailList.setLayoutManager(new LinearLayoutManager(mContext));
        mDBind.detailList.setAdapter(mAdapter);
    }

    @Override
    protected void initData() {
        super.initData();
        int defaultSize = DefaultDaoUtil.getInstance().findDefaultByDevice(mDevice).size();
        if (defaultSize == 0){
            DefaultDaoUtil.getInstance().setDefaultList(mContext, smartDevice.getDevCommon(), mDevice);
        }
        doStatusShow(mDevice.getDevice_status());
        showView();
    }

    @Override
    protected void initListener() {
        super.initListener();
        mDBind.operation.setOnClickListener(view -> {
            String mTestCode;
            switch (smartDevice){
                case EE_DEV_SX_SM_ALARM:
                    mTestCode = "17000000";
                    break;
                case EE_DEV_FLASH_ALARM:
                    mTestCode = "AA000000";
                    break;
                default:
                    mTestCode = "BB000000";
                    break;

            }
            mSendEquipment.sendEquipmentCommand(mDevice.getDevice_ID(), mTestCode);
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        if(TextUtils.isEmpty(mDevice.getSubdeviceName())){
            String title = getString(smartDevice.getTypeStrId()) + mDevice.getDevice_ID();
            mDBind.detailName.setText(title);
        }else{
            mDBind.detailName.setText(mDevice.getSubdeviceName());
        }
        refreshDefaultList();
    }

    private void showView() {
        switch (smartDevice){
            case EE_DEV_CO_ALARM:
            case EE_DEV_SM_ALARM:
            case EE_DEV_GAS_ALARM:
            case EE_DEV_NEW_GAS_ALARM:
            case EE_DEV_CO_GAS_ALARM:
            case EE_DEV_SX_SM_ALARM:
            case EE_DEV_WT_ALARM:
            case EE_DEV_THERM_ALARM:
            case EE_DEV_FLASH_ALARM:
                showOperation();
                break;
            case EE_DEV_MODE_BUTTON:
                mAdapter.setChange(true);
                break;
        }
    }

    private void doStatusShow(String statusCode) {
        try {
            String signal = statusCode.substring(0,2);
            mDBind.signalPosition.setImageResource(ConfigureUtil.choseSPic(signal));

            String status = DeviceStatusUtil.getAlert(mContext, mDevice.getDevice_type(), statusCode);
            int statusColor = DeviceStatusUtil.getStatus(mDevice.getDevice_type(), statusCode);
            int quantity = Integer.parseInt(statusCode.substring(2,4),16);
            showDetail(statusColor, status, quantity);
        }catch (Exception e){
            showDetail(3, getString(R.string.offline), 0);
        }
    }

    private void showDetail(int statusColor, String text, int quantity){
        if (statusColor == 3){
            mDBind.signalPosition.setImageResource(ConfigureUtil.choseSPic("00"));
            mDBind.quantityPosition.setVisibility(View.GONE);
            mDBind.quantityDesc.setVisibility(View.GONE);
        }else {
            mDBind.quantityPosition.setVisibility(View.VISIBLE);
            mDBind.quantityDesc.setVisibility(View.VISIBLE);
            mDBind.quantityPosition.setImageResource(ConfigureUtil.choseQPic(quantity));
            mDBind.quantityDesc.setText(ConfigureUtil.choseLNum(quantity));
        }
        int background = ConfigureUtil.choseDeviceStateColor(statusColor);
        mDBind.detailLayout.setBackgroundResource(background);
        mDBind.showStatus.setText(text);
        if (statusColor == 0) {
            AnimationDrawable mAnimation = (AnimationDrawable) mDBind.detailLayout.getBackground();
            mAnimation.start();
        }
    }

    private void showOperation(){
        mDBind.detailTest.setVisibility(View.VISIBLE);
        try {
            int ds = Integer.parseInt(mDevice.getDevice_type().substring(mDevice.getDevice_type().length()-1),16);
            if(ds<7){
                mDBind.operation.setVisibility(View.VISIBLE);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    protected void refreshUI(String status) {
        super.refreshUI(status);
        doStatusShow(status);
    }

    @Override
    protected void refreshDefaultList() {
        super.refreshDefaultList();
        List<DefaultBean> defaultBeanList = DefaultDaoUtil.getInstance().findDefaultByDevice(mDevice);
        mAdapter.setList(defaultBeanList);
    }
}
